package com.kust.smarthome.web.controller;

import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.model.UserAddress;
import com.kust.smarthome.web.service.IUserAddressService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.AddressVo;
import com.kust.smarthome.web.vo.UserAddressVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import java.security.Principal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author iAuzre
 * @since 2021-07-09
 */
@RestController
@RequestMapping("/userAddress")
@Api(value = "用户收货地址相关操作", tags = {"shopping-tag"})
public class UserAddressController {

    @Resource
    private IUserAddressService userAddressService;

    @Resource
    private IUserService userService;

    /**
     * 添加收货信息
     * @param principal 用户信息
     * @param addressVo 收货信息表单封装类
     * @return 返回添加是否成功的 bool 值
     */
    @GetMapping("/add")
    @ApiOperation(value = "添加收货地址", notes = "用户通过填写表单AddressVo进行添加收货地址操作")
    public boolean add(Principal principal, AddressVo addressVo) {

        User user = userService.getByUsername(principal.getName());

        List<UserAddress> list = userAddressService.selectByUserId(user.getId());

        Integer check=1;

        if (list!=null){
            check=0;
        }

        UserAddress userAddress = new UserAddress()
                .setUserId(user.getId())
                .setName(addressVo.getName())
                .setAddressIntro(addressVo.getAddressIntro())
                .setPhone(addressVo.getPhone())
                .setProvince(addressVo.getProvince())
                .setMunicipality(addressVo.getMunicipality())
                .setDistrict(addressVo.getDistrict())
                .setAbsolutePosition(check);

        int num = userAddressService.insert(userAddress);

        return num == 1;
    }

    /**
     * 用户可查看个人的收货信息
     * @param principal 用户信息
     * @return 返回对应用户信息list
     */
    @GetMapping("/selectByUserId")
    @ApiOperation(value = "收货信息查询", notes = "用户查看自己的收货信息内容")
    public List<UserAddress> selectByUserId(Principal principal) {

        User user = userService.getByUsername(principal.getName());

        return userAddressService.selectByUserId(user.getId());

    }

    /**
     * 此处为用户修改收货地址,表单中所有数据都需要做非空验证
     * @param principal 用户信息
     * @param userAddressVo 表单信息封装类
     * @return 返回信息
     */
    @GetMapping("/updateById")
    @ApiOperation(value = "修改收货信息", notes = "用户提供对应表单进行修改操作")
    public String updateById(Principal principal, UserAddressVo userAddressVo) {

        User user = userService.getByUsername(principal.getName());

        String msg = userAddressService.updateUserAddress(user.getId(), userAddressVo);

        return msg;
    }

    @GetMapping("/getById")
    @ApiOperation("通过id获取地址信息表")
    public UserAddress getById(Integer id) {
        return userAddressService.getById(id);
    }

    @GetMapping("/setAbsolutePosition")
    @ApiOperation("通过UserId确定唯一的一个默认地址")
    public boolean absolutePosition(Principal principal,Integer id){
        User user = userService.getByUsername(principal.getName());

        return userAddressService.absoluteAddress(user.getId(),id);
    }

    @GetMapping("/getAbsoluteAddress")
    @ApiOperation("获取默认地址")
    public UserAddress getAbsoluteAddress(Principal principal) {
        User user = userService.getByUsername(principal.getName());

        return userAddressService.getAbsoluteAddress(user.getId());
    }

    @GetMapping("/delete")
    @ApiOperation("删除收货地址")
    public boolean deleteaddress(Principal principal,Integer addressId){
        User user = userService.getByUsername(principal.getName());
        return userAddressService.deleteById(user.getId(),addressId);
    }

}
